import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { WeappVersionService } from '../../services/admin/weapp-version.service';

/**
 * WeappVersionController
 * 对应 PHP: WeappVersion Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('wxoplatform')
@Controller('adminapi/wxoplatform')
export class WeappVersionController {
  constructor(private readonly weappVersionService: WeappVersionService) {}

  /**
   * 平台提交小程序版本
   * 路由: POST weapp/version/commit
   * PHP路由: Route::post('weapp/version/commit', 'wxoplatform.WeappVersion/weappCommit')
   */
  @Post('weapp/version/commit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '平台提交小程序版本' })
  async weappCommit(@Body() data: WeappCommitDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: weappCommit

      return await this.weappVersionService.add(data);
    } catch (error) {
      throw new BusinessException('weappCommit操作失败', error);
    }
  }

  /**
   * getSiteGroupCommitRecord
   * 路由: GET sitegroup/commit
   * PHP路由: Route::get('sitegroup/commit', 'wxoplatform.WeappVersion/getSiteGroupCommitRecord')
   */
  @Get('sitegroup/commit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'getSiteGroupCommitRecord' })
  async getSiteGroupCommitRecord(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getSiteGroupCommitRecord

      return await this.weappVersionService.getSiteGroupCommitRecord();
    } catch (error) {
      throw new BusinessException('getSiteGroupCommitRecord操作失败', error);
    }
  }

  /**
   * 平台提交小程序版本
   * 路由: GET weapp/commit/last
   * PHP路由: Route::get('weapp/commit/last', 'wxoplatform.WeappVersion/lastCommitRecord')
   */
  @Get('weapp/commit/last')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '平台提交小程序版本' })
  async lastCommitRecord(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lastCommitRecord

      return await this.weappVersionService.getLastCommitRecord();
    } catch (error) {
      throw new BusinessException('lastCommitRecord操作失败', error);
    }
  }

  /**
   * commitRecord
   * 路由: GET weapp/commit
   * PHP路由: Route::get('weapp/commit', 'wxoplatform.WeappVersion/commitRecord')
   */
  @Get('weapp/commit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'commitRecord' })
  async commitRecord(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: commitRecord

      return await this.weappVersionService.getPage(data);
    } catch (error) {
      throw new BusinessException('commitRecord操作失败', error);
    }
  }

  /**
   * 平台提交小程序版本
   * 路由: POST site/weapp/commit
   * PHP路由: Route::post('site/weapp/commit', 'wxoplatform.WeappVersion/siteWeappCommit')
   */
  @Post('site/weapp/commit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '平台提交小程序版本' })
  async siteWeappCommit(
    @Body() data: SiteWeappCommitDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: siteWeappCommit

      return await this.weappVersionService.siteWeappCommit();
    } catch (error) {
      throw new BusinessException('siteWeappCommit操作失败', error);
    }
  }

  /**
   * 平台提交小程序版本
   * 路由: PUT undo/weappaudit
   * PHP路由: Route::put('undo/weappaudit', 'wxoplatform.WeappVersion/undoAudit')
   */
  @Put('undo/weappaudit')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '平台提交小程序版本' })
  async undoAudit(@Body() data: UndoAuditDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: undoAudit

      return await this.weappVersionService.undoAudit(data['id']);
    } catch (error) {
      throw new BusinessException('undoAudit操作失败', error);
    }
  }

  /**
   * 平台提交小程序版本
   * 路由: POST async/siteweapp
   * PHP路由: Route::post('async/siteweapp', 'wxoplatform.WeappVersion/syncSiteWeapp')
   */
  @Post('async/siteweapp')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '平台提交小程序版本' })
  async syncSiteWeapp(@Body() data: SyncSiteWeappDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: syncSiteWeapp

      return await this.weappVersionService.syncSiteGroupAuthWeapp(
        data['site_group_id'],
      );
    } catch (error) {
      throw new BusinessException('syncSiteWeapp操作失败', error);
    }
  }
}
